package com.hd.crdao.activity;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.PurchasedMealAdapter;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.PurchasedMealBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/7/18 0018.
 * <p>
 * 我购买的套餐界面
 */
public class PurchasedMealActivity extends BaseActivity {
    @BindView(R.id.head)
    ExpandHeadView mHead;
    @BindView(R.id.report_count)
    TextView mReportCount;
    @BindView(R.id.listview_meal)
    MyListView mListviewMeal;
    @BindView(R.id.progreassbar)
    ProgressBar mProgreassbar;
    public PurchasedMealAdapter mPurchasedMealAdapter;
    public Handler mHandler = new Handler();
    public String mCarAccountId;
    public int mCaTimes;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_purchased_meal;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
//        String businessid = SPUtils.getString("businessid", "");
        mCarAccountId = getIntent().getStringExtra("carAccountId");
        mCaTimes = getIntent().getIntExtra("CaTimes", 0);
        mReportCount.setText("报告剩余"+mCaTimes+"条");
        loadData(mCarAccountId);
    }

    @Override
    protected void initListener() {
        mHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListviewMeal.setonRefreshListener(new MyListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new LoadRunnale(),1500);
            }
        });
    }

    /**
     * 根据接口请求数据
     */
    private void loadData(String carAccountId) {
        OkHttpUtils.post().url(Constants.PURCHASED_MEAL_URL).addParams("sign", Constants.SIGN).addParams("carAccountId", carAccountId)
                .addParams("serviceTypeId", "1").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("请求失败,请检查网络...");
                mProgreassbar.setVisibility(View.GONE);
            }

            @Override
            public void onResponse(String response, int id) {
                mProgreassbar.setVisibility(View.GONE);
                Gson gson = new Gson();
                PurchasedMealBean purchasedMealBean = gson.fromJson(response, PurchasedMealBean.class);
                String success = purchasedMealBean.getSuccess();
                String message = purchasedMealBean.getMessage();
                List<PurchasedMealBean.DataBean> data = purchasedMealBean.getData();
                if(TextUtils.equals("00",success)){
                    if(data!= null){
                        mPurchasedMealAdapter = new PurchasedMealAdapter(data);
                        mListviewMeal.setAdapter(mPurchasedMealAdapter);
                    }
                }else {
                    MyToast.show(message);
                }
            }
        });
    }

    class LoadRunnale implements Runnable {
        @Override
        public void run() {
            //加载数据
            loadData(mCarAccountId);
            if(mPurchasedMealAdapter !=null){
                mPurchasedMealAdapter.notifyDataSetChanged();
            }
            mListviewMeal.onRefreshComplete();
        }
    }

}
